package com.tzq.autoticket.utils;

import com.tzq.autoticket.Config;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.Calendar;

public class WebUtils {
    private static final int WAIT = 75;

    public static WebElement getUserName(WebDriver driver) {
        while (true) {
            try {
                return driver.findElement(By.id("usid"));
            } catch (Exception e) {
//                e.printStackTrace();
                try {
                    Thread.sleep(WAIT);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                    break;
                }
            }
        }
        return null;
    }

    public static int getYear(WebDriver driver) {
        while (true) {
            try {
                WebElement yearElement = driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/div/div[@class='ui-datepicker-title']/span[@class='ui-datepicker-year']"));
                Log.info(yearElement.getText());
                return Integer.parseInt(yearElement.getText());
            } catch (Exception e) {
//                e.printStackTrace();
                try {
                    Thread.sleep(WAIT);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                    break;
                }
            }
        }
        return -1;
    }

    public static int getMonth(WebDriver driver) {
        while (true) {
            try {
                WebElement monthElement = driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/div/div[@class='ui-datepicker-title']/span[@class='ui-datepicker-month']"));
                Log.info(monthElement.getText());
                return parseMonth(monthElement.getText());
            } catch (Exception e) {
//                e.printStackTrace();
                try {
                    Thread.sleep(WAIT);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                    break;
                }
            }
        }
        return -1;
    }

    public static WebElement getUploadElement(WebDriver driver) {
        while (true) {
            try {
                WebElement monthElement = driver.findElement(By.id("teamFile"));
                Log.info(monthElement.getText());
                return monthElement;
            } catch (Exception e) {
//                e.printStackTrace();
                try {
                    Thread.sleep(WAIT);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                    break;
                }
            }
        }
        return null;
    }

    public static Alert waitAlert(WebDriver driver) {
        while (true) {
            try {
                return driver.switchTo().alert();
            } catch (Exception e) {
//                e.printStackTrace();
                try {
                    Thread.sleep(WAIT);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                    break;
                }
            }
        }
        return null;
    }

    public static WebElement waitNextButton(WebDriver driver) {
        while (true) {
            try {
                return driver.findElement(By.id("button_book"));
            } catch (Exception e) {
//                e.printStackTrace();
                try {
                    Thread.sleep(WAIT);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                    break;
                }
            }
        }
        return null;
    }

    public static WebElement waitLinpiaoren(WebDriver driver) {
        while (true) {
            try {
                return driver.findElement(By.xpath("//input[@name='06001ornm']"));
            } catch (Exception e) {
//                e.printStackTrace();
                try {
                    Thread.sleep(WAIT);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                    break;
                }
            }
        }
        return null;
    }

    public static WebElement waitRefreshbtn(WebDriver driver) {
        while (true) {
            try {
                return driver.findElement(By.id("listPdno")).findElement(By.tagName("li")).findElement(By.tagName("div")).findElement(By.tagName("img"));
            } catch (Exception e) {
//                e.printStackTrace();
                try {
                    Thread.sleep(WAIT);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                    break;
                }
            }
        }
        return null;
    }

    public static WebElement waitDayComeIn(WebDriver driver) {
        while (true) {
            try {
                return driver.findElement(By.xpath("//div[@id='ui-datepicker-div']")).findElement(By.partialLinkText("" + Config.getInstance().getCalendar().get(Calendar.DAY_OF_MONTH)));
            } catch (Exception e) {
//                e.printStackTrace();
                try {
                    Thread.sleep(WAIT);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                    break;
                }
            }
        }
        return null;
    }

    public static WebElement waitYuPiaoElement(WebDriver driver, WebElement dayElement) {
        while (true) {
            try {
                return dayElement.findElement(By.xpath("../span"));
            } catch (Exception e) {
//                e.printStackTrace();
                try {
                    Thread.sleep(WAIT);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                    break;
                }
            }
        }
        return null;
    }

    public static boolean waitCheckOk(WebDriver driver) {
        while (true) {
            try {
                WebElement element = driver.findElement(By.className("yidun_tips__text"));
                if (element.getText().contains("验证成功")) {
                    return true;
                }
            } catch (Exception e) {
//                e.printStackTrace();
                try {
                    Thread.sleep(WAIT);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                    break;
                }
            }
        }
        return false;
    }

    public static WebElement waitNextBt(WebDriver driver) {
        while (true) {
            try {
                return driver.findElement(By.id("next_bt"));
            } catch (Exception e) {
//                e.printStackTrace();
                try {
                    Thread.sleep(WAIT);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                    break;
                }
            }
        }
        return null;
    }

    public static boolean waitPayDone(WebDriver driver) {
        while (true) {
            try {
                if (driver.getPageSource().contains("请牢记您的订单号码")) {
                    return true;
                }
            } catch (Exception e) {
//                e.printStackTrace();
                try {
                    Thread.sleep(WAIT);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                    break;
                }
            }
        }
        return false;
    }

    private static int parseMonth(String month) {
        int m = 0;
        switch (month) {
            case "一月":
                m = 0;
                break;
            case "二月":
                m = 1;
                break;
            case "三月":
                m = 2;
                break;
            case "四月":
                m = 3;
                break;
            case "五月":
                m = 4;
                break;
            case "六月":
                m = 5;
                break;
            case "七月":
                m = 6;
                break;
            case "八月":
                m = 7;
                break;
            case "九月":
                m = 8;
                break;
            case "十月":
                m = 9;
                break;
            case "十一月":
                m = 10;
                break;
            case "十二月":
                m = 11;
                break;


        }
        return m;

    }
}
